package com.android.ailaoshan.sound;


import android.os.Environment;

import com.android.ailaoshan.common.ResourceInfo;
import com.android.ailaoshan.location.LastLocation;

import org.litepal.LitePal;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 声音管理
 */
class SoundManager {
    private final String SOUND_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator +"AiLaoShan"+ File.separator + "Sound" + File.separator;

    /**
     * 删除声音文件
     */
    void deleteSoundFile(String fileName){
        new Thread(
            () -> {
                // 删除本地文件
                String filePath = SOUND_PATH + fileName;
                File file = new File(filePath);
                if(file.exists()){
                    file.delete();
                }
                // 删除记录
                LitePal.deleteAll(ResourceInfo.class,"name = ?",file.getName());
            }
        ).start();
    }


    /**
     * 批量删除文件
     */
    void deleteAllSoundFile(ArrayList<String> allFiles){
        new Thread(
            () -> {
                for(String file : allFiles){
                    String filePath = SOUND_PATH + file;
                    File targetFile = new File(filePath);
                    if(targetFile.exists()){
                        targetFile.delete();
                    }
                    // 删除记录
                    LitePal.deleteAll(ResourceInfo.class,"name = ?",targetFile.getName());
                }
            }
        ).start();
    }


    /**
     * 保存文件信息
     */
    void saveSoundInfo(ArrayList<String> fileList, int protect, String description, int saveType){
        new Thread(
            () -> {
                if(saveType == 1){
                    // 所属批次
                    long fileNo = System.currentTimeMillis();
                    // 经纬度
                    HashMap<String,Double> geoInfo = LastLocation.getLocation();
                    // 记录时间
                    long date = System.currentTimeMillis();

                    // 保存图片
                    for( int i=0; i<fileList.size(); i++){
                        ResourceInfo resourceInfo = new ResourceInfo();
                        resourceInfo.setName(fileList.get(i));
                        resourceInfo.setDate(date);
                        resourceInfo.setProtect(protect);

                        if(geoInfo != null){
                            resourceInfo.setLat(geoInfo.get("lat"));
                            resourceInfo.setLng(geoInfo.get("lng"));
                        }
                        resourceInfo.setFileNo(fileNo);
                        resourceInfo.setDescribe(description);
                        resourceInfo.setFileType(2);
                        resourceInfo.setUpload(2);

                        // 保存信息
                        resourceInfo.save();
                    }
                }else{
                    List<ResourceInfo> resourceInfoList = LitePal
                            .where("name=?",fileList.get(0))
                            .find(ResourceInfo.class);
                    if(resourceInfoList.size() > 0){
                        ResourceInfo resourceInfo = resourceInfoList.get(0);
                        long fileNo = resourceInfo.getFileNo();
                        String deccribe = resourceInfo.getDescribe();
                        double lat = resourceInfo.getLat();
                        double lng = resourceInfo.getLng();
                        int xProtect = resourceInfo.getProtect();
                        long xDate = resourceInfo.getDate();

                        LitePal.deleteAll(ResourceInfo.class,"fileNo = ?",fileNo +"");

                        for(String name : fileList){
                            ResourceInfo uResourceInfo = new ResourceInfo();

                            uResourceInfo.setName(name);
                            uResourceInfo.setUpload(2);
                            uResourceInfo.setLng(lng);
                            uResourceInfo.setLat(lat);
                            uResourceInfo.setFileType(2);
                            uResourceInfo.setFileNo(fileNo);
                            uResourceInfo.setDescribe(deccribe);
                            uResourceInfo.setProtect(xProtect);
                            uResourceInfo.setDate(xDate);

                            uResourceInfo.save();
                        }
                    }else{
                        saveSoundInfo(fileList,protect,description,1);
                    }
                }
            }
        ).start();
    }
}
